package com.jiangnan.warehouse_management.controller;


import com.jiangnan.warehouse_management.common.Result;
import com.jiangnan.warehouse_management.dto.request.CreateWarehouse;
import com.jiangnan.warehouse_management.dto.request.WarehouseQueryRequest;
import com.jiangnan.warehouse_management.dto.request.WarehouseRequest;
import com.jiangnan.warehouse_management.dto.response.WarehouseQueryResponse;
import com.jiangnan.warehouse_management.pojo.Warehouse;
import com.jiangnan.warehouse_management.service.WarehouseService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Validated
public class WarehouseController {
    @Autowired
    private final WarehouseService warehouseService;

    @GetMapping("/warehousesInfo")
    Result<WarehouseQueryResponse> getWarehousesInfo(@Validated @ModelAttribute WarehouseQueryRequest request) {
        WarehouseQueryResponse data = new WarehouseQueryResponse();
        data = warehouseService.getWarehousesInfo(request);
        return Result.success(data);
    }

    @PostMapping("/createWarehouses")
    Result<Warehouse> createWarehouses(@Validated @RequestBody CreateWarehouse request) {
        Warehouse data = new Warehouse();
        data = warehouseService.createWarehouse(request);
        return Result.success(data);
    }

    @GetMapping("/warehouseById/{id}")
    Result<WarehouseQueryResponse.Warehouses> warehousesById(@PathVariable("id") int id) {
        WarehouseQueryResponse.Warehouses data = warehouseService.warehousesById(id);
        return Result.success(data);
    }

    @PutMapping("/updateWarehouse/{id}")
    Result<Warehouse> updateWarehouse(@PathVariable("id") int id, @RequestBody WarehouseRequest request) {
        Warehouse data = warehouseService.updateWarehouse(id, request);
        return Result.success(data);
    }

    @DeleteMapping("/deleteWarehouse/{id}")
    Result<?> deleteWarehouse(@PathVariable("id") int id) {
        warehouseService.deleteWarehouse(id);
        return Result.success();
    }
}
